
import config from '../../utils/config.js'
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var Auth = require('../../utils/auth.js');

var wxApi = require('../../utils/wxApi.js')
var wxRequest = require('../../utils/wxRequest.js');
const Adapter = require('../../utils/adapter.js')
var webSiteName = config.getWebsiteName;
var domain = config.getDomain
var app = getApp();
Page({
    data: {
        isActive: null,
        listMain: [{
            id: "1",
            region: "A",
            items: [{
                id: "..",
                name: "阿明"
            }, {
                id: "..",
                name: "阿乐"
            }, {
                id: "..",
                name: "奥特曼"
            }, {
                id: "..",
                name: "安庆"
            }]
        }, {
            id: "2",
            region: "B",
            items: [{
                id: "..",
                name: "爸爸"
            }, {
                id: "..",
                name: "八仔"
            }]
        }, {
            id: "3",
            region: "C",
            items: [{
                id: "..",
                name: "车仔面"
            }, {
                id: "..",
                name: "吃货"
            }, {
                id: "..",
                name: "蠢货"
            }]
        }, {
            id: "4",
            region: "D",
            items: [{
                id: "..",
                name: "担担面"
            }, {
                id: "..",
                name: "刀仔"
            }, {
                id: "..",
                name: "兑兑"
            }]
        }, {
            id: "5",
            region: "E",
            items: [{
                id: "..",
                name: "担担面"
            }, {
                id: "..",
                name: "刀"
            }, {
                id: "..",
                name: "刀"
            }, {
                id: "..",
                name: "刀"
            }, {
                id: "..",
                name: "刀"
            }, {
                id: "..",
                name: "刀"
            }, {
                id: "..",
                name: "刀"
            }, {
                id: "..",
                name: "刀"
            }, {
                id: "..",
                name: "刀"
            }, {
                id: "..",
                name: "对对"
            }]
        }, {
            id: "6",
            region: "F",
            items: [{
                id: "..",
                name: "冯洁"
            }, {
                id: "..",
                name: "峰仔"
            }, {
                id: "..",
                name: "凤姐"
            }]
        },],
        fixedTitle: null,
        toView: 'inTo0',
        oHeight: [],
        scroolHeight: 0,
        fixedTop: 0,
        tab_index_0: true,
        tab_index_1: false,
        tab_index_2: false
    },
    //点击右侧字母导航定位触发
    scrollToViewFn: function (e) {
        var that = this;
        var _id = e.target.dataset.id;
        for (var i = 0; i < that.data.listMain.length; ++i) {
            if (that.data.listMain[i].id === _id) {
                that.setData({
                    isActive: _id,
                    toView: 'inTo' + _id,
                    fixedTitle: that.data.listMain[i].region
                })
                break;
            }
        }
    },
    // 页面滑动时触发
    onPageScroll: function (e) {
        this.setData({
            scroolHeight: e.detail.scrollTop
        });
        // console.log(e.detail.scrollTop);
        for (let i in this.data.oHeight) {
            // console.log(this.data.oHeight[i].height);
            if (e.detail.scrollTop < this.data.oHeight[i].height) {
                this.setData({
                    isActive: this.data.oHeight[i].key,
                    fixedTitle: this.data.oHeight[i].name
                });
                return false;
            }
        }
    },
    // 处理数据格式，及获取分组高度
    getBrands: function () {
        var that = this;
        var number = 0
        //计算分组高度,wx.createSelectotQuery()获取节点信息
        for (let i = 0; i < that.data.listMain.length; ++i) {
            wx.createSelectorQuery().select('#inTo' + that.data.listMain[i].id).boundingClientRect(function (rect) {
                number = rect.height + number;
                var newArry = [{
                    'height': number,
                    'key': rect.dataset.id,
                    "name": that.data.listMain[i].region
                }]
                that.setData({
                    oHeight: that.data.oHeight.concat(newArry)
                })
            }).exec();
        };
    },
    onLoad: function (options) {
        var that = this;
        that.getBrands();
        var brand = wxRequest.getRequest(Api.getCarBrand());
        brand.then(res => {
            that.setData({
                listMain: res.data,
            });
        });
    },
    tabNavChange: function (options) {
        let that = this;
        // return false;
        //切换菜单
        let selectd_index = options.detail.index;
        switch (selectd_index) {
            case 0:
                that.setData({
                    tab_index_0: true,
                    tab_index_1: false,
                    tab_index_2: false,
                });
                break;
            case 1:
                that.setData({
                    tab_index_0: false,
                    tab_index_1: true,
                    tab_index_2: false,
                });
                break;
            case 2:
                that.setData({
                    tab_index_0: false,
                    tab_index_1: false,
                    tab_index_2: true,
                });
                break;
        }
    },
    brand_model: function (options) {
      
        let brand_id = options.currentTarget.dataset.id;
        let brand_name = options.currentTarget.dataset.name;
        //跳转其他页面
        //敬请期待
        // this.$showDialog({
        //   content: '敬请期待',
        //   showCancel: false,
        //   success: res => {
        //       console.log(res);
        //   }
        // }); 
        //return false;
        wx.navigateTo({
            url: '../topicModel/model?brand_id='+brand_id+'&brand_name='+brand_name,
        })
    },
    goToWindow:function(event){
        const { index } = event.currentTarget.dataset;
        const url = `/pages/carWindow/carWindow?index=${index}`;
        wx.navigateTo({ url });
    }
})